﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dogshow.BusinessLogic.Managers;
using Dogshow.Infrastructure.Exceptions;
using Dogshow.Model;
using Dogshow.Web.Areas.Korisnik.Models;
using Dogshow.Web.Infrastructure;
using Dogshow.Web.Models;
using Dogshow.Web.Settings;
using Dogshow.Web.Helpers;
using Status = Dogshow.Web.Models.Status;

namespace Dogshow.Web.Areas.Korisnik.Controllers
{
    public class PsiController : DogshowController
    {
        private readonly PasManager m_PasManager;

        private readonly KorisniciManager m_KorisniciManager;

        public PsiController(PasManager pasManager, KorisniciManager korisniciManager)
        {
            m_PasManager = pasManager;
            m_KorisniciManager = korisniciManager;
        }

        public ActionResult MojiPsi()
        {
            try
            {
                var mojiPsi = m_PasManager.VratiPseKorisnika(Request.Cookies.VratiKorisnika().KorisnickoIme);
                return View("MojiPsiView", new MojiPsiViewModel { Psi = mojiPsi });
            }
            catch (Exception exc)
            {
                DodajStatus(new Greska(exc));
                return View("MojiPsiView", new MojiPsiViewModel());
            }
        }

        public ActionResult Pas(int id)
        {
            var pas = m_PasManager.VratiPsa(id);
            PodesavanjeComboBoxaZaOcaPsa();
            PodesavanjeComboBoxaZaMajku();
            PodesavanjaComboBoxaVlasnikaPsa();
            PodesavanjaComboBoxaOdgajivacaPsa();
            return View("PasView", pas);
        }

        public ActionResult UnosPsa()
        {
            PodesavanjeComboBoxaZaOcaPsa();
            PodesavanjeComboBoxaZaMajku();
            PodesavanjaComboBoxaVlasnikaPsa();
            PodesavanjaComboBoxaOdgajivacaPsa();
            var korisnik =
                m_KorisniciManager.VratiKorisnikaPoKorisnickomImenu(Request.Cookies.VratiKorisnika().KorisnickoIme);
            return View("PasView", new Pas{IdVlasnika = korisnik.IdKinologa});
        }

        public ActionResult SacuvajPsa(Pas pas)
        {
            PodesavanjeComboBoxaZaOcaPsa();
            PodesavanjeComboBoxaZaMajku();
            PodesavanjaComboBoxaVlasnikaPsa();
            PodesavanjaComboBoxaOdgajivacaPsa();
            if (!ModelState.IsValid)
            {
                return View("PasView", pas);
            }
            try
            {
                m_PasManager.SacuvajPsa(pas);
                DodajStatus(new UspenoSnimanje());
                return RedirectToAction("UnosPsa");
            }
            catch(PasBezVlasnikaException exc)
            {
                DodajStatus(new Status(StatusType.Warning, exc.Message));
                try
                {
                    pas = m_PasManager.VratiPsaPoBrojuRodovnika(pas.BrojRodovnika);
                }
                catch (Exception exc1)
                {
                    DodajStatus(new Greska(exc1));
                }
                return View("PasView", pas);
            }
            catch (ValidationException exc)
            {
                DodajStatus(new PodaciNisuValidni());
                ModelState.AddModelError(exc.PropertyInError, exc.Message);
                return View("PasView", pas);
            }
        }

        public JsonResult VratiPse()
        {
            var psi = m_PasManager.VratiSvePse();
            return Json(psi, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult OtacPsaComboBoxCallback()
        {
            var podesavanja = PodesavanjeComboBoxaZaOcaPsa();
            ViewData["settings"] = podesavanja;
            return PartialView("_PasComboBox");
        }

        public PartialViewResult MajkaPsaComboBoxCallback()
        {
            var podesavanja = PodesavanjeComboBoxaZaMajku();
            ViewData["settings"] = podesavanja;
            return PartialView("_PasComboBox");
        }

        public PartialViewResult RasePasaComboBoxCallback()
        {
            return PartialView("_RasePasaComboBox");
        }

        public PartialViewResult VlasnikPsaComboBox()
        {
            ViewData["settings"] = PodesavanjaComboBoxaVlasnikaPsa();
            return PartialView("_KinologComboBox");
        }

        public PartialViewResult OdgajivacPsaComboBox()
        {
            ViewData["settings"] = PodesavanjaComboBoxaOdgajivacaPsa();
            return PartialView("_KinologComboBox");
        }

        private PasComboBoxSettings PodesavanjeComboBoxaZaOcaPsa()
        {
            var settings = new PasComboBoxSettings
            {
                Name = "IdOca",
                CallbackRouteValues = new { Controller = "Psi", Action = "OtacPsaComboBoxCallback" },
                ButtonClick = "pasComboBoxButtonClick"
            };
            ViewData["OtacPsaComboBoxSettings"] = settings;
            return settings;
        }

        private PasComboBoxSettings PodesavanjeComboBoxaZaMajku()
        {
            var settings = new PasComboBoxSettings
            {
                Name = "IdMajke",
                CallbackRouteValues = new { Controller = "Psi", Action = "MajkaPsaComboBoxCallback" },
                ButtonClick = "pasComboBoxButtonClick"
            };
            ViewData["MajkaPsaComboBoxSettings"] = settings;
            return settings;
        }

        private KinologComboBoxSettings PodesavanjaComboBoxaVlasnikaPsa()
        {
            var settings = new KinologComboBoxSettings
            {
                Name = "IdVlasnika",
                CallbackRouteValues = new { Controller = "Psi", Action = "VlasnikPsaComboBox" },
                ButtonClick = "otvoriUnosKinologa"
            };
            ViewBag.PodesavanjaComboBoxaVlasnikaPsa = settings;
            return settings;
        }

        private KinologComboBoxSettings PodesavanjaComboBoxaOdgajivacaPsa()
        {
            var settings = new KinologComboBoxSettings
            {
                Name = "IdOdgajivaca",
                CallbackRouteValues = new { Controller = "Psi", Action = "OdgajivacPsaComboBox" },
                ButtonClick = "otvoriUnosKinologa"
            };
            ViewBag.PodesavanjaComboBoxaOdgajivacaPsa = settings;
            return settings;
        }

    }
}
